<template>
  <section class="m-card">
    <div class="top-title">
      <p>
        <el-page-header v-if="hasBack && canBack"
                        @back="goBack"> </el-page-header>
        <slot name="title"><i class="el-icon-guide"
             v-if="!hasBack || !canBack"></i>{{title || $route.meta.title}}</slot>
      </p>
      <div class="btn">
        <el-button size="mini"
                   v-if="isShow"
                   @click="show=!show">
          {{show?'隐藏':'展开'}}更多筛选
          <i v-if="!show"
             class="el-icon-caret-bottom"></i>
          <i v-if="show"
             class="el-icon-caret-top"></i>
        </el-button>
        <el-button type='primary'
                   size="mini"
                   plain
                   @click="search">查询</el-button>
        <el-button type='info'
                   size="mini"
                   plain
                   @click="reset">重置</el-button>
      </div>
    </div>
    <div class="default"
         ref="wrap"
         :class="{'hidden':!show}">
      <slot />
    </div>
  </section>
</template>
<script>
import QueryMixins from '@/utils/query'
import { debounce } from 'lodash'
export default {
  mixins: [QueryMixins],
  name: 'ym-query-view',
  props: {
    params: {
      default: 'form'
    },
    model: {
      type: Object,
      default: () => { }
    },
    title: {
      default: ''
    },
    hasBack: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    $route (v) {
      if (v && !Object.keys(v.query).length) {
        this.reset()
      }
    }
  },
  computed: {
    isShow () {
      return this.childrenHeight > 62
    },
    canBack () {
      return window.history.length > 1
    }
  },
  data () {
    return {
      show: false,
      childrenHeight: 0
    }
  },
  mounted () {
    this.getChildrenHeight()
    window.addEventListener('resize', () => {
      this.getChildrenHeight()
    })
  },
  methods: {
    search () {
      this.$emit('search', 'search')
    },
    reset () {
      this.$parent[this.params] = {}
      this.$nextTick(() => {
        this.search()
      })
      this.$emit('reset', 'reset')
    },
    // 获取所有子级 dom 高度
    getChildrenHeight: debounce(function () {
      let childrenHeight = ''
      this.$nextTick(function () {
        if (this.$refs['wrap']) {
          Array.from(this.$refs['wrap'].childNodes, el => {
            childrenHeight += el.offsetHeight
          })
          this.childrenHeight = childrenHeight
        }
      })
    }, 500),
    goBack () {
      this.$router.go(-1)
    }
  }
}
</script>
<style lang="scss" scoped>
/deep/.title-control {
  padding-bottom: 0 !important;
}
.top-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  line-height: 38px;
  border-bottom: 1px solid #f5f5f5;
  font-size: 16px;
  p {
    font-weight: bolder;
    display: flex;
    align-items: center;
    i {
      margin-right: 6px;
      font-size: 20px;
    }
  }
  &.hidden {
    margin-right: 10px;
    transform: rotate(-90deg);
  }
}
.default {
  padding-top: 10px;
  &.hidden {
    height: 50px;
    overflow: hidden;
  }
  /deep/.el-form-item {
    .el-input__inner {
      width: 250px;
    }
  }
}
.icon {
  margin-right: 10px;
  transform: rotate(90deg);
}
</style>
